using Microsoft.AspNetCore.Mvc;
using JTZY.Model.Goods.Dto;
using JTZY.Model.Goods;
using JTZY.Service.Goods.IGoodsService;
using JTZY.Admin.WebApi.Filters;
using JTZY.Model.Content.Dto;
using JTZY.Model;
using System.Threading.Tasks;

//创建时间：2024-11-20
namespace JTZY.Admin.WebApi.Controllers.Goods
{
    /// <summary>
    /// checkstock
    /// </summary>
    [Verify]
    [Route("goods/Stocklogmaster")]
    public class StocklogmasterController : BaseController
    {
        /// <summary>
        /// checkstock接口
        /// </summary>
        private readonly IStocklogmasterService _StocklogmasterService;

        public StocklogmasterController(IStocklogmasterService StocklogmasterService)
        {
            _StocklogmasterService = StocklogmasterService;
        }

        /// <summary>
        /// 查询checkstock列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "stocklogmaster:list")]
        public IActionResult QueryStocklogmaster([FromQuery] StocklogmasterQueryDto parm)
        {
            parm.userId = HttpContext.GetUId();
            var response = _StocklogmasterService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询checkstock详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "stocklogmaster:query")]
        public IActionResult GetStocklogmaster(long Id)
        {
            var response = _StocklogmasterService.GetInfo(HttpContext.GetUId(),Id);
            return SUCCESS(response);
        }

        /// <summary>
        /// 添加checkstock
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "stocklogmaster:add")]
        [Log(Title = "checkstock", BusinessType = BusinessType.INSERT)]
        public IActionResult AddStocklogmaster([FromBody] StocklogmasterDto parm)
        {
            var modal = parm.Adapt<Stocklogmaster>().ToCreate(HttpContext);

            var response = _StocklogmasterService.AddStocklogmaster(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新checkstock
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "stocklogmaster:edit")]
        [Log(Title = "checkstock", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateStocklogmaster([FromBody] StocklogmasterDto parm)
        {
            var modal = parm.Adapt<Stocklogmaster>().ToUpdate(HttpContext);
            var response = _StocklogmasterService.UpdateStocklogmaster(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除checkstock
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete/{ids}")]
        [ActionPermissionFilter(Permission = "stocklogmaster:delete")]
        [Log(Title = "checkstock", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteStocklogmaster([FromRoute]string ids)
        {
            var idArr = Tools.SplitAndConvert<int>(ids);

            return ToResponse(_StocklogmasterService.Delete(idArr));
        }
        /// <summary>
        /// 删除checkstock
        /// </summary>
        /// <returns></returns>
        [HttpGet("getTree")]
        [ActionPermissionFilter(Permission = "stocklogmaster:getTree")]
        [Log(Title = "checkstock", BusinessType = BusinessType.OTHER)]
        public IActionResult GetCategoryTree()
        {
            long userId = HttpContext.GetUId();
            return SUCCESS(_StocklogmasterService.GetCategoryTree(userId));
        }

        [HttpPost("queryStockProdByProdId")]
        [Log(Title = "checkstock", BusinessType = BusinessType.OTHER)]
        public IActionResult QueryStockProdByProdId([FromBody] StockPdRangeQueryDto parm)
        {
            parm.userId = HttpContext.GetUId();

            return SUCCESS(_StocklogmasterService.QueryStockProdByProdId(parm));
        }
        [HttpPost("savePdSheet")]
        [Log(Title = "checkstock", BusinessType = BusinessType.OTHER)]
        public IActionResult SavePdSheet([FromBody] StockPdSheetDto parm)
        {
            long userId = HttpContext.GetUId();
            return SUCCESS(_StocklogmasterService.SavePdSheet(userId,parm));
        }

        [HttpPost("editPdSheet")]
        [Log(Title = "checkstock", BusinessType = BusinessType.OTHER)]
        public IActionResult EditPdSheet([FromBody] StockPdSheetDto parm)
        {
            long userId = HttpContext.GetUId();
            return SUCCESS(_StocklogmasterService.EditPdSheet(userId, parm));
        }

        [HttpGet("approvePdSheet/{masterId}")]
        [Log(Title = "checkstock", BusinessType = BusinessType.OTHER)]
        public IActionResult ApprovePdSheet(long masterId)
        {
            long userId = HttpContext.GetUId();
            return SUCCESS(_StocklogmasterService.ApprovePdSheet(userId, masterId));
        }
        /// <summary>
        /// 删除checkstock
        /// </summary>
        /// <returns></returns>
        [HttpGet("delete/{id}")]
        [ActionPermissionFilter(Permission = "stocklogmaster:deletePdSheet")]
        [Log(Title = "checkstock", BusinessType = BusinessType.DELETE)]
        public IActionResult DelPdSheet(long id)
        {
            long userId = HttpContext.GetUId();

            return SUCCESS(_StocklogmasterService.DelPdSheet(userId,id));
        }

        [HttpGet("hasNotCheckedPdSheet")]
        [ActionPermissionFilter(Permission = "stocklogmaster:getTree")]
        [Log(Title = "checkstock", BusinessType = BusinessType.OTHER)]
        public IActionResult HasNotCheckedPdSheet()
        {
            long userId = HttpContext.GetUId();
            return SUCCESS(_StocklogmasterService.HasNotCheckedPdSheet(userId));
        }


        /// <summary>
        /// 导出盘点报告
        /// </summary>
        /// <returns></returns>
        [Log(Title = "盘点报告", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [HttpGet("export")]
        [ActionPermissionFilter(Permission = "stocklogmaster:export")]
        public IActionResult ExportReport([FromQuery] StocklogmasterQueryDto parm)
        {
            long userId = HttpContext.GetUId();
            parm.PageNum = 1;
            parm.PageSize = 100000;
            var list = _StocklogmasterService.ExportList(userId, parm).Result;
            if (list == null || list.Count <= 0)
            {
                return ToResponse(ResultCode.FAIL, "没有要导出的数据");
            }
            var result = ExportExcelMini(list, "盘点报告", (!string.IsNullOrEmpty(list.First().goodsName)? (list.First().goodsName):"")+"盘点报告");
            return ExportExcel(result.Item2, result.Item1);
        }

        [Log(Title = "盘点列表", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [HttpGet("exportList")]
        [ActionPermissionFilter(Permission = "stocklogmaster:export")]
        public IActionResult ExportList([FromQuery] StocklogmasterQueryDto parm)
        {
            long userId = HttpContext.GetUId();
            parm.PageNum = 1;
            parm.PageSize = 100000;
            var list = _StocklogmasterService.ExportMasterList(userId, parm).Result;
            if (list == null || list.Count <= 0)
            {
                return ToResponse(ResultCode.FAIL, "没有要导出的数据");
            }
            var result = ExportExcelMini(list, "本店盘点记录", "本店盘点记录");
            return ExportExcel(result.Item2, result.Item1);
        }
        [Log(Title = "盘点获取物码信息", BusinessType = BusinessType.OTHER, IsSaveResponseData = false)]
        [HttpGet("getProdInfo")]
        [ActionPermissionFilter(Permission = "stocklogmaster:getProdInfo")]
        public async Task<IActionResult> GetProdInfo(string snCode)
        {
            long userId = HttpContext.GetUId();
            var result = await _StocklogmasterService.GetProdInfo(snCode, userId);
            if (!result.status)
            {
                return ToResponse(ResultCode.FAIL, result.msg);
            }
            return SUCCESS(result.data);
        }
    }
}